package com.empire.module.member.controller.admin.hotelrecord;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.hotelrecord.vo.*;
import com.empire.module.member.dal.dataobject.hotelrecord.HotelRecordDO;
import com.empire.module.member.service.hotelrecord.HotelRecordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 旅馆住宿")
@RestController
@RequestMapping("/member/hotel-record")
@Validated
public class HotelRecordController {

    @Resource
    private HotelRecordService hotelRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建旅馆住宿")
    @PreAuthorize("@ss.hasPermission('member:hotel-record:create')")
    public CommonResult<Long> createHotelRecord(@Valid @RequestBody HotelRecordSaveReqVO createReqVO) {
        return success(hotelRecordService.createHotelRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅馆住宿")
    @PreAuthorize("@ss.hasPermission('member:hotel-record:update')")
    public CommonResult<Boolean> updateHotelRecord(@Valid @RequestBody HotelRecordSaveReqVO updateReqVO) {
        hotelRecordService.updateHotelRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅馆住宿")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:hotel-record:delete')")
    public CommonResult<Boolean> deleteHotelRecord(@RequestParam("id") Long id) {
        hotelRecordService.deleteHotelRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅馆住宿")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:hotel-record:query')")
    public CommonResult<HotelRecordRespVO> getHotelRecord(@RequestParam("id") Long id) {
        HotelRecordDO hotelRecord = hotelRecordService.getHotelRecord(id);
        return success(BeanUtils.toBean(hotelRecord, HotelRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅馆住宿分页")
//    @PreAuthorize("@ss.hasPermission('member:hotel-record:query')")
    public CommonResult<PageResult<HotelRecordRespVO>> getHotelRecordPage(@Valid HotelRecordPageReqVO pageReqVO) {
        PageResult<HotelRecordDO> pageResult = hotelRecordService.getHotelRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HotelRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅馆住宿 Excel")
    @PreAuthorize("@ss.hasPermission('member:hotel-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHotelRecordExcel(@Valid HotelRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HotelRecordDO> list = hotelRecordService.getHotelRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅馆住宿.xls", "数据", HotelRecordRespVO.class,
                        BeanUtils.toBean(list, HotelRecordRespVO.class));
    }

}